package com.acargil.sudoku.model.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

import com.acargil.sudoku.model.IStandardSudokuEngine;

public class SudokuUtil {

	public static IStandardSudokuEngine load(InputStream inputStream) {
		ObjectInputStream objectInputStream;
		IStandardSudokuEngine model = null;
		
		try {
			objectInputStream = new ObjectInputStream(inputStream);
			try {
				model = (IStandardSudokuEngine) objectInputStream.readObject();
			} catch (Exception e) {
				model = null;
			} finally {
				objectInputStream.close();
			}
		} catch (IOException e1) { }
		
		return model;
	}

	public static boolean save(IStandardSudokuEngine model, OutputStream outputStream) {
		boolean result = false;
		ObjectOutputStream objectOutputStream;
		try {
			objectOutputStream = new ObjectOutputStream(outputStream);
			try {
				objectOutputStream.writeObject(model);
				result = true;
			} catch (Exception e) {
				result = false;
			} finally {
				outputStream.close();
			}
		} catch (IOException e) {	}
		
		return result;
	}

}
